using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;

namespace ConcurrantLoadingLib
{
    public class Trades : IEnumerable<Trade>
    {
        private readonly object _tradesLock = new object();
        private readonly List<Trade> _trades = new List<Trade>();

        public void Add(Trade trade)
        {
            lock(_tradesLock)
            {
                Console.WriteLine("Adding order {0} with thread {1}", trade.OrderId, Thread.CurrentThread.ManagedThreadId);
                _trades.Add(trade);
            }
        }

        public IEnumerator<Trade> GetEnumerator()
        {
            return new SafeEnumerator<Trade>(_trades.GetEnumerator(), _tradesLock);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}